//
//  NSDate+CM_DateTools.m
//  CoolMove
//
//  Created by CA on 15/10/30.
//  Copyright © 2015年 CA. All rights reserved.
//

#import "NSDate+CM_DateTools.h"

@implementation NSDate (CM_DateTools)

- (NSString *)convertMonthDayByWeekDay:(WeekDay)weekDay
{
    NSInteger dayNumber = self.weekday;
    if (dayNumber == 1) {
        dayNumber += 7;
    }
    
    NSInteger differDay = dayNumber - 2;
    
    NSDate *mondayDate = [self dateBySubtractingDays:differDay];
    NSDate *convertDayDate = [mondayDate dateByAddingDays:weekDay - 1];
    
    NSString *convertDayStr = [convertDayDate formattedDateWithFormat:@"MM/dd"];
    
    return convertDayStr;
}


/**
 * @brief 获取两个日期之间的天数
 * @return    总天数
 */
+ (NSInteger)numberOfDaysWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitDay
                                         fromDate:fromDate
                                           toDate:toDate
                                          options:NSCalendarWrapComponents];
    return labs(comp.day);
}

/**
 * @brief 获取两个日期之间的相差的月份
 * @return    总月份数
 */
+ (NSInteger)numberOfMonthsWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitMonth
                                         fromDate:fromDate
                                           toDate:toDate
                                          options:NSCalendarWrapComponents];
    return labs(comp.month);
}


@end
